package edu.task.repository;

import edu.task.model.SysTaskSubtask;
import edu.task.model.SysTaskSubtaskPK;
import org.apache.ibatis.annotations.Param;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface SysTaskSubtaskRepository extends JpaRepository<SysTaskSubtask, SysTaskSubtaskPK> {
    @Modifying
    @Query(value = " delete ts from sys_task_subtask as ts " +
            "inner join sys_task as t on ts.task_id = t.task_id " +
            "inner join sys_subtask as s on s.subtask_id = ts.subtask_id " +
            "where ts.task_id=:taskId ", nativeQuery = true)
    void deleteTaskSubtaskByTaskId(Long taskId);

    @Query(value = " select distinct ts.subtask_id from sys_task_subtask as ts " +
            "inner join sys_task as t on ts.task_id = t.task_id " +
            "inner join sys_subtask as s on ts.subtask_id = s.subtask_id " +
            "where ts.task_id=:taskId", nativeQuery = true)
    List<Long> selectSubtaskIdByTaskId(@Param("taskId") Long taskId);
}
